﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ShoppingList
{
    public partial class Lists : PhoneApplicationPage
    {
        public Lists()
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            (ApplicationBar.Buttons[2] as ApplicationBarIconButton).IsEnabled = App.ViewModel.Lists.Count > 1;
        }

        private void SelectItem_Click(object sender, RoutedEventArgs e)
        {
            var clicked = ((sender as Button).DataContext as ListViewModel);
            if (clicked != null)
            {
                App.ViewModel.CurrentList = clicked;
                Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 20));
            }
            NavigationService.GoBack();
        }

        private void ApplicationBarAddItemButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddListPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarEditItemButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/EditListPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarDeleteButton_Click(object sender, EventArgs e)
        {
            if (App.ViewModel.Lists.Count > 1 && App.ViewModel.CurrentList != null &&
                Common.ConfirmDialog(ShoppingList.Resources.ConfirmDeletion, String.Format(ShoppingList.Resources.DeleteListQuestion, App.ViewModel.CurrentList.Name)))
            {
                App.ViewModel.Lists.Remove(App.ViewModel.CurrentList);
                App.ViewModel.CurrentList = App.ViewModel.Lists.OrderBy(l => l.Date).First();
            }
        }

        private void ApplicationBarSaveButton_Click(object sender, EventArgs e)
        {
            App.ViewModel.SyncWithDropBox();
        }

        private void ApplicationBarMenuItemLoadSampleData_Click(object sender, EventArgs e)
        {
            if (Common.ConfirmDialog(ShoppingList.Resources.ConfirmLoadTitle, ShoppingList.Resources.ConfirmLoadQuestion))
            {
                App.ViewModel.LoadSampleData();
                ListsList.SelectedItem = App.ViewModel.CurrentList;
            }
        }

        private void ApplicationBarEmailButton_Click(object sender, EventArgs e)
        {
            App.ViewModel.CurrentList.SendViaEmail();
        }
    }
}